﻿using FDSimpleModelGenerator.Entities;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace FDSimpleModelGenerator.IRepositories
{
    /// <summary>
    /// 基础访问
    /// 主键基于int
    /// </summary>
    public interface IBasicRepository<T> where T : IEntity, new()
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        Task<bool> AddAsync(T model);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync(T model);

        /// <summary>
        /// 新增或更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        Task<bool> AddOrUpdateAsync(T model);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync(int id);

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<T> GetAsync(int id);

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        Task<IList<T>> GetListAsync();

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        bool Add(T model);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        bool Update(T model);

        /// <summary>
        /// 新增或更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        bool AddOrUpdate(T model);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        bool Delete(int id);

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T Get(int id);

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        IList<T> GetList();

        int ExecuteNoneQuery(string sql, object param = null);
        TModel ExecutScalar<TModel>(string sql, object param = null);
        TModel Query<TModel>(string sql, object param = null);

        Task<int> ExecuteNoneQueryAsync(string sql, object param = null);
        Task<TModel> ExecutScalarAsync<TModel>(string sql, object param = null);
        Task<TModel> QueryAsync<TModel>(string sql, object param = null);


        /// <summary>
        /// 开始事务
        /// </summary>
        /// <returns></returns>
        Task<IDbConnection> BeginTransaction();

        /// <summary>
        /// 提交事务
        /// </summary>
        /// <returns></returns>
        Task<bool> CommitTransaction();

        /// <summary>
        /// 回滚事务
        /// </summary>
        /// <returns></returns>
        Task<bool> RollbackTransaction();
    }
}
